package com.lib.common.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @Author rames
 * @date 2018/12/1 15:43
 */
public class SPUtils {
    // 创建一个写入器
    private static SharedPreferences mPreferences;
    private static SharedPreferences.Editor mEditor;
    private static SPUtils mSharedPreferencesUtil;

    public static final String TOKEN = "APP_TOKEN";
    public static final String IS_LOGINED = "IS_LOGINED";
    public static final String USER_INFO = "USER_INFO";
    public static final String APP_SPU = "SPU_UTILS";
    public static final String USER_ID = "USER_ID";

    // 构造方法
    public SPUtils(Context context) {
        mPreferences = context.getSharedPreferences(APP_SPU, Context.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    // 单例模式
    public static SPUtils getInstance(Context context) {
        if (mSharedPreferencesUtil == null) {
            mSharedPreferencesUtil = new SPUtils(context);
        }
        return mSharedPreferencesUtil;
    }

    // 存入数据
    public void putString(String key, String value) {
        LogUtil.e("==========> key: "+key+" value: "+value);
        mEditor.putString(key, value);
        mEditor.commit();
    }

    // 获取数据
    public String getString(String key) {
        return mPreferences.getString(key, "");
    }

    // 获取数据
    public String getString(String key, String defaultValue) {
        return mPreferences.getString(key, defaultValue);
    }

    // 移除数据
    public void removeKey(String key) {
        mEditor.remove(key);
        mEditor.commit();
    }

    // 存入数据
    public void putInt(String key, int value) {
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    // 获取数据
    public int getInt(String key) {
        return mPreferences.getInt(key, -1);
    }

    public void putBoolean(String key, boolean value){
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    public boolean getBoolean(String key){
        return mPreferences.getBoolean(key, false);
    }
}
